const fs=require('fs');
const express=require('express');
const session=require('express-session');
const app=express();
app.listen(3000,()=>{console.log('启动了')});

app.use(session({
	secret:'asdfsdfadf',
	saveUninitialized:true,
	resave:true,
}))
app.get('/index.html',(req,res)=>{
	fs.readFile('./public/index.html','utf-8',(err,data)=>{
		let title='咏鹅';
		let author='骆冰王';
		let content='鹅鹅鹅，曲项向天歌';
		
		data=data.replace('{{title}}',title);
		data=data.replace('{{author}}',author);
		data=data.replace('{{content}}',content);
		res.send(data);
	});
});
app.get('/login.html',(req,res)=>{
	fs.readFile('./public/login.html','utf-8',(err,data)=>{
		res.send(data);
	});
});

app.use(express.urlencoded({extended:false}));
app.post('/api/login',(req,res)=>{
	//console.log(req.body.username,req.body.password);
	if(req.body.username=='admin' && req.body.password=='123'){
		req.session.isLogin='abc';
		req.session.username='老汤';
		res.send({status:200,msg:'登录成功'});
	}else{
		res.send({status:500,msg:'登录失败'});
	}
});